package com.huiqiao.lesson9.controller;

import com.huiqiao.lesson9.entity.SysUser;
import com.huiqiao.lesson9.entity.dto.User;
import com.huiqiao.lesson9.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user/v2")
@Api(tags = "用户管理接口")
public class SwaggerUserController extends BaseController {

    @Autowired
    private SysUserService userService;

    @ApiOperation("获取用户信息")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, example = "1")
    public AppResponse<SysUser> getUser(@PathVariable Long id) {
        SysUser user = new SysUser();
        user.setUserId(id);
        return success(user);
    }

    @ApiOperation("获取用户列表")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = false, example = "陈峰"),
            @ApiImplicitParam(name = "email", value = "邮箱 ", required = false, example = "664839088@qq.com"),
    })
    public AppResponse<List<SysUser>> getUserList(@RequestParam(required = false) String name, @RequestParam(required = false) String email) {
        SysUser user = new SysUser();
        user.setUserName(name);
        user.setEmail(email);
        List<SysUser> list = new ArrayList();
        list.add(user);
        return success(list);
    }


    @ApiOperation("新增用户")
    @PostMapping
    public AppResponse<SysUser> addUser(@RequestBody User user) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(user, sysUser);
        sysUser.setUserId(100L);
        return success(sysUser);
    }

    @ApiOperation("更新用户信息")
    @PutMapping
    public AppResponse<SysUser> udpateUser(@RequestBody User user) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(user, sysUser);
        sysUser.setUserName("更新一下");
        return success(sysUser);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, example = "1")
    public AppResponse<Void> addUser(@PathVariable Integer id) {
        return success();
    }
}
